package cn.iocoder.yudao.module.exam.controller.admin.studentpaper;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.exam.controller.admin.studentpaper.vo.*;
import cn.iocoder.yudao.module.exam.dal.dataobject.studentpaper.StudentPaperDO;
import cn.iocoder.yudao.module.exam.service.studentpaper.StudentPaperService;

@Tag(name = "管理后台 - 试卷管理")
@RestController
@RequestMapping("/exam/student-paper")
@Validated
public class StudentPaperController {

    @Resource
    private StudentPaperService studentPaperService;

    @PostMapping("/create")
    @Operation(summary = "创建试卷管理")
    @PreAuthorize("@ss.hasPermission('exam:student-paper:create')")
    public CommonResult<Long> createStudentPaper(@Valid @RequestBody StudentPaperSaveReqVO createReqVO) {
        return success(studentPaperService.createStudentPaper(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新试卷管理")
    @PreAuthorize("@ss.hasPermission('exam:student-paper:update')")
    public CommonResult<Boolean> updateStudentPaper(@Valid @RequestBody StudentPaperSaveReqVO updateReqVO) {
        studentPaperService.updateStudentPaper(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除试卷管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('exam:student-paper:delete')")
    public CommonResult<Boolean> deleteStudentPaper(@RequestParam("id") Long id) {
        studentPaperService.deleteStudentPaper(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除试卷管理")
                @PreAuthorize("@ss.hasPermission('exam:student-paper:delete')")
    public CommonResult<Boolean> deleteStudentPaperList(@RequestParam("ids") List<Long> ids) {
        studentPaperService.deleteStudentPaperListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得试卷管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('exam:student-paper:query')")
    public CommonResult<StudentPaperRespVO> getStudentPaper(@RequestParam("id") Long id) {
        StudentPaperDO studentPaper = studentPaperService.getStudentPaper(id);
        return success(BeanUtils.toBean(studentPaper, StudentPaperRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得试卷管理分页")
    @PreAuthorize("@ss.hasPermission('exam:student-paper:query')")
    public CommonResult<PageResult<StudentPaperRespVO>> getStudentPaperPage(@Valid StudentPaperPageReqVO pageReqVO) {
        PageResult<StudentPaperDO> pageResult = studentPaperService.getStudentPaperPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StudentPaperRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出试卷管理 Excel")
    @PreAuthorize("@ss.hasPermission('exam:student-paper:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStudentPaperExcel(@Valid StudentPaperPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StudentPaperDO> list = studentPaperService.getStudentPaperPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "试卷管理.xls", "数据", StudentPaperRespVO.class,
                        BeanUtils.toBean(list, StudentPaperRespVO.class));
    }

}